from django.contrib.staticfiles.views import serve
from django.db.models import Q
from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView

from books.models import BooksModel, TypeModel, CreateAddressModel, CommitModel, WriterModel
from books.ser import BooksSer, TypeSer, CreAddressSer, CommitSer, WriterSer
from user.models import UserModel
from utils.authentication import MyAuthentication


# Create your views here.
class Page(PageNumberPagination):
    page_size = 5
    page_size_query_param = 'size'
    page_query_param = 'page'

#插图书列表
class FindBooks(ListAPIView):
    queryset = BooksModel.objects.all()
    serializer_class = BooksSer
    pagination_class = Page
    def get_queryset(self):
        query=Q()
        typename=self.request.query_params.get('typename')
        name=self.request.query_params.get('name')
        writername=self.request.query_params.get('writername')
        createname=self.request.query_params.get('createname')
        if typename:
            query&=Q(typename=typename)
        if name:
            query&=Q(bookname__icontains=name)
        if writername:
            query&=Q(writername__writername__icontains=writername)
        if createname:
            query&=Q(createname=createname)
        return self.queryset.filter(query)

#只展示前3条数据
class FindBooks1(ListAPIView):
    queryset = BooksModel.objects.all()[:3]
    serializer_class = BooksSer
    pagination_class = Page

#只展示3条数据
class FindBooks2(ListAPIView):
    queryset = BooksModel.objects.all().order_by('count')[:2]
    serializer_class = BooksSer
    pagination_class = Page

#只展示后4条数据
class FindBooks3(ListAPIView):
    queryset = BooksModel.objects.all()[2:5]
    serializer_class = BooksSer
    pagination_class = Page

#查图书分类
class FindType(ListAPIView):
    queryset = TypeModel.objects.all()
    serializer_class = TypeSer

#查出版社
class FindCreAddress(ListAPIView):
    queryset = CreateAddressModel.objects.all()
    serializer_class = CreAddressSer

#图书详情
class BooksDetail(APIView):
    def get(self, request):
        id = request.query_params.get('id')
        book=BooksModel.objects.filter(id=id).first()
        ser=BooksSer(book)
        return Response({"msg":"ok","data":ser.data})

#评价列表
class CommitList(APIView):
    def get(self,request):
        bookid=request.query_params.get('id')
        commit=CommitModel.objects.filter(bookid=bookid)
        ser=CommitSer(commit,many=True)
        return Response({"msg":"ok","data":ser.data})

#添加评价
class CommitAdd(APIView):
    def post(self,request):
        bookid=request.data.get('bookid')
        userid=request.data.get('userid')
        commitname=request.data.get('commitname')
        commitcontent=request.data.get('commitcontent')
        committime=request.data.get('committime')
        data={
            "bookid":bookid,
            "userid":userid,
            "commitname":commitname,
            "commitcontent":commitcontent,
            "committime":committime,
        }
        ser=CommitSer(data=data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"添加成功","code":200})
        else:
            return Response(ser.errors)

# #删除评价
# class CommitDel(APIView):
#     def delete(self,request):
#         id=request.query_params.get('id')
#         CommitModel.objects.filter(id=id).delete()
#         return Response({"msg":"删除成功","code":200})

#管理员添加图书
class BooksAdd(APIView):
    def post(self,request):
        ser=BooksSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"msg":"添加成功","code":200})
        else:
            return Response(ser.errors)

#查询作者
class FindWriter(ListAPIView):
    queryset = WriterModel.objects.all()
    serializer_class = WriterSer


